package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.constant.WorkOrderConstant;
import com.tbit.main.dao.core.WorkOrderFinishingDao;
import com.tbit.main.pojo.*;
import com.tbit.main.pojo.VO.WorkOrderFinishingCountRespVO;
import com.tbit.main.pojo.VO.WorkOrderFinishingQueryReqVO;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author LMY
 * @create 2021-05-18 15:39
 * 工单
 */
@Service("workOrderFinishingService")
public class WorkOrderFinishingServiceImpl implements WorkOrderFinishingService {

    @Autowired
    private WorkOrderFinishingDao workOrderFinishingDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private RoleService roleService;


    @Override
    public void insert(WorkOrder workOrder) {
        workOrderFinishingDao.insert(workOrder);
    }

    @Override
    public List<WorkOrderFinishing> getByAccountId(Integer accountId) {
        return workOrderFinishingDao.getByAccountId(accountId);
    }

    @Override
    public List<WorkOrderFinishing> getByAccountIdAndWorkType(Integer accountId, Integer workType, Integer socLimit) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("workType", workType);
        map.put("socLimit", socLimit);
        return workOrderFinishingDao.getByAccountIdAndWorkType(map);
    }

    @Override
    public void del(String workOrderId) {
        workOrderFinishingDao.del(workOrderId);
    }

    @Override
    public void updateState(String workOrderId, Integer accountUserId, String accountUserName, Integer state) {
        Map<String, Object> map = new HashMap<>();
        if (state.equals(WorkOrderConstant.state_LQ)) {
            map.put("workOrderId", workOrderId);
            map.put("getUserId", accountUserId);
            map.put("getUserName", accountUserName);
            map.put("getTime", DateTimeUtil.getNowTime());
            map.put("state", state);
        } else if (state.equals(WorkOrderConstant.state_WC)) {
            map.put("workOrderId", workOrderId);
            map.put("finishTime", DateTimeUtil.getNowTime());
            map.put("state", state);
        }  else if (state.equals(WorkOrderConstant.state_SHZ)) {
            map.put("workOrderId", workOrderId);
            map.put("state", state);
        }
        workOrderFinishingDao.updateState(map);
    }

    @Override
    public List<WorkOrderFinishing> getByAccountIdUserId(Integer accountId, AccountUser accountUser, String startTime, String endTime, Integer state, Integer workType) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put(DaoParamConstant.startTime, startTime);
        map.put(DaoParamConstant.endTime, endTime);
        map.put("state", state);
        map.put("workType", workType);

        if (roleService.getRoleCode(accountUser.getAccountUserType()).equals(1)) {
            map.put("getUserId", accountUser.getAccountUserId());
        }
        return workOrderFinishingDao.getByAccountIdUserId(map);
    }

    @Override
    public Integer getByUserIdCount(Integer accountId, AccountUser accountUser, String startTime, String endTime, Integer state, Integer workType) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put(DaoParamConstant.startTime, startTime);
        map.put(DaoParamConstant.endTime, endTime);
        map.put("state", state);
        map.put("workType", workType);

        if (roleService.getRoleCode(accountUser.getAccountUserType()).equals(1)) {
            map.put("getUserId", accountUser.getAccountUserId());
        }
        return workOrderFinishingDao.getByUserIdCount(map);
    }

    @Override
    public List<WorkOrderSta> getGroupByUserId(Integer accountId, Integer userType, Integer accountUserId, String startTime, String endTime, Integer state, Integer workType, List<Integer> regionId, Integer isValid) {
        List<WorkOrderSta> dlses = new ArrayList<WorkOrderSta>();

        /**查询代理商运维账号*/
        List<Integer> accountUserIds = new ArrayList<Integer>();
        /**判断当前账号是不是运维人员*/
        if (roleService.getRoleCode(userType).equals(1)) {
            accountUserIds.add(accountUserId);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("userMainIds", StringUtil.getListString(accountUserIds));
        params.put("state", state);
        params.put("workType", workType);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        params.put("isValid", isValid);
        if (CollUtil.isEmpty(regionId)) {
            params.put("exist", "'exist'");
            regionId = georeGionService.getByAccountUserId(accountUserId);
        } else {
            params.put("exist", null);
        }
        params.put("regionId", StringUtil.getListString(regionId));
        dlses = workOrderFinishingDao.groupByTime(params);
        List<WorkOrderSta> dlsefilters = new ArrayList<WorkOrderSta>();
        if (dlses.size() > 0) {
            for (WorkOrderSta dls : dlses) {
                AccountUser au = accountUserService.getById(dls.getUserMainId());
                if (au != null) {
                    dls.setName(au.getName());
                    dls.setPhone(au.getPhone());
                    dlsefilters.add(dls);
                }
            }
        }
        return dlsefilters;
    }

    @Override
    public List<WorkOrderFinishing> getByUserId(Integer accountId, AccountUser accountUser, Integer userMainId,
                                                Integer state, Integer workType,
                                                List<Integer> regionId, Integer machineType, Integer buildType, Integer locationType) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("state", state);
        map.put("workType", workType);
        map.put("machineType", machineType);
        map.put("locationType", locationType);
        if (!state.equals(WorkOrderConstant.state_CJ)) {
            map.put("getUserId", userMainId);
        }
        if (CollUtil.isEmpty(regionId)) {
            map.put("exist", "'exist'");
            regionId = georeGionService.getByAccountUserId(accountUser.getAccountUserId());
        } else {
            map.put("exist", null);
        }
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("buildType", buildType);
        return workOrderFinishingDao.getByUserId(map);
    }

    @Override
    public WorkOrderFinishing getByMachineIdWorkType(Integer machineId, Integer workType, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineId", machineId);
        map.put("workType", workType);
        map.put(DaoParamConstant.accountId, accountId);
        return workOrderFinishingDao.getByMachineIdWorkType(map);
    }

    @Override
    public List<WorkOrderFinishing> getByMachineId(Integer machineId) {
        return workOrderFinishingDao.getByMachineId(machineId);
    }

    @Override
    public List<WorkOrderFinishing> getByBuildTime(String buildTime) {
        return workOrderFinishingDao.getByBuildTime(buildTime);
    }

    @Override
    public List<String> getByGetTime(String getTime) {
        return workOrderFinishingDao.getByGetTime(getTime);
    }

    @Override
    public void delWorkOrderIds(List<String> workOrderIds) {
        workOrderFinishingDao.delWorkOrderIds(StringUtil.getStringListString(workOrderIds));
    }

    @Override
    public List<Integer> getByWorkType(Integer workType) {
        return workOrderFinishingDao.getByWorkType(workType);
    }

    @Override
    public WorkOrderFinishing getByWorkOrderId(String workOrderId) {
        return workOrderFinishingDao.getByWorkOrderId(workOrderId);
    }

    @Override
    public void update(WorkOrder workOrder) {
        workOrderFinishingDao.update(workOrder);
    }

    @Override
    public void delBatch(List<String> workOrderIds) {
        List<List<String>> workOrder = ListUtils.partition(workOrderIds, 900);
        for (List<String> workOrderId : workOrder) {
            workOrderFinishingDao.delBatch(StringUtil.getArrayListString(workOrderId));
        }
    }

    @Override
    public List<WorkOrderFinishing> getAll() {
        return workOrderFinishingDao.getAll();
    }

    @Override
    public WorkOrderFinishing getByWorkMachineIdState(Integer workType, Integer machineId, Integer state) {
        Map<String, Object> map = new HashMap<>();
        map.put("workType", workType);
        map.put("machineId", machineId);
        map.put("state", state);
        return workOrderFinishingDao.getByWorkMachineIdState(map);
    }

    @Override
    public List<WorkOrderFinishing> getByWorkMachineIdState(Integer machineId, Integer state) {
        return workOrderFinishingDao.selectByWorkMachineIdState(machineId, state);
    }

    @Override
    public Integer getUserIdWorkType(Integer mainUserId, Integer workType) {
        Map<String, Object> map = new HashMap<>();
        map.put("mainUserId", mainUserId);
        map.put("workType", workType);
        return workOrderFinishingDao.getUserIdWorkType(map);
    }

    @Override
    public List<WorkOrderFinishing> getWorkOrderFinishingList(WorkOrderFinishingQueryReqVO reqVO) {
        LogUtil.info(String.format("[getWorkOrderFinishingList][请求参数: %s]", reqVO));
        return workOrderFinishingDao.selectWorkOrderFinishingList(reqVO);
    }

    @Override
    public WorkOrderFinishingCountRespVO getWorkOrderFinishingCount(WorkOrderFinishingCountRespVO workOrderFinishingCountAll, List<WorkOrderFinishing> workOrderFinishingList) {
        // 查询工种信息并根据人员分组
        WorkOrderFinishingCountRespVO workOrderFinishingCount = new WorkOrderFinishingCountRespVO();
        workOrderFinishingList.forEach(workOrderFinishing ->
            workOrderFinishingCount.add(workOrderFinishingCountAll, workOrderFinishing));
        return workOrderFinishingCount;
    }

    @Override
    public Integer getUserIdWorkTypeLocationType(Integer mainUserId, Integer workType, Integer locationType) {
        Map<String,Object> map = new HashMap<>();
        map.put("mainUserId",mainUserId);
        map.put("workType",workType);
        map.put("locationType",locationType);
        return workOrderFinishingDao.getUserIdWorkType(map);
    }
}
